package com.andendginegame;

import java.util.HashMap;

import org.anddev.andengine.entity.scene.Scene;

import android.widget.Toast;

import com.andendginegame.AndEngineGame.holeType;

public class GameManager {
	
	private static final int MAX_TIME_BY_PLAYER = 60;
	private static final int MAX_TIME_BY_LOST_CLOSE = 10;
	private static GameManager gameManager = null;
	public static int GAME_MODE_CLASSIC = 1;
	int numHoles = 0;
	int numRows = 0;
	int numColumns = 0;
	int numHolesByColumns = 0;
	int numHolesByRows = 0;
	int holesPressed = 0;
	private int holeWidthSepar = 0;
	private int holeHeightSepar = 0;
	private int gameMode;
	HashMap<String, Hole> holes = null;
	public static int numLostClose = 0;
	public boolean multiTouchSupported = false;
	private int timeByPlayer = 0;
	private int timeForLostClosed = 0;
	
	public static GameManager getInstance(int gameMode){
		if (gameManager == null){
			gameManager = new GameManager(gameMode);
		}
		return gameManager;
	}
	
	public static GameManager getInstance(){
		if (gameManager == null){
			gameManager = new GameManager(GAME_MODE_CLASSIC);
		}
		return gameManager;
	}
	
	
	public GameManager(int gameMode) {
		if (gameMode == GAME_MODE_CLASSIC){
			this.gameMode = gameMode;
			gameModeClassicInit();
		}
	}
	
	public void gameModeClassicInit() {
		numHoles = 30;
		numRows = 5;
		numColumns = 6 ;
		numHolesByRows = 6;
		numHolesByColumns = 5;
		holeWidthSepar = AndEngineGame.CAMERA_WIDTH / numColumns;
		holeHeightSepar = AndEngineGame.CAMERA_HEIGHT / numRows;
		
	}
	
	public void createBoard(Scene scene){
		if (gameMode == GAME_MODE_CLASSIC){
			createBoardClassic(scene);
		}
	}

	private void createBoardClassic(Scene scene) {
		holes = new HashMap<String, Hole>(numHoles);
		holeType[] holeValues = holeType.values();
		int typeInt=0;
		double random;
		float x = 0,y = 0;
		for (int i = 0; i < numRows; i++) {
			if (i == 0){
				y = holeHeightSepar / 2;
			}else{
				y+=holeHeightSepar;	
			}
			for (int j = 0; j < numColumns; j++) {

				if (j == 0){
					x = holeWidthSepar / 2;
				}else{
					x+=holeWidthSepar;
					
				}
				random = Math.random();
				typeInt=(int)Math.abs(random*holeValues.length);
				addHole(x, y, 0, holeValues[typeInt], scene);
//				addHole(x, y, 0, holeValues[(int) (i*j)%7], scene);
				
//				if (j == 0)
//					addHole(x, y, 0, holeValues[(int) Math.random()*holeValues.length+1], scene);
//				else
//					addHole(x, y, 0, AndEngineGame.holeType.BLUE, scene);
				
			}
		}

	}
	
	private void addHole(float x,float y, int status, holeType type, Scene scene) {
		if (holes == null){
			holes = new HashMap<String, Hole>(numHoles);
		}
		Hole holeAux = new Hole(x, y, status, AndEngineGame.getmHoleTextures().get(type), AndEngineGame.getmHoleTextures().get(type), scene);
		holes.put(holeAux.getId(), holeAux);
		
	}

	public static int getNumLostClose() {
		return numLostClose;
	}
	
	public static int addNumLostClose() {
		return numLostClose++;
	}
	
	public static int lessNumLostClose() {
		return numLostClose--;
	}
	
	

	public static void setNumLostClose(int numLostClose) {
		GameManager.numLostClose = numLostClose;
	}

	public boolean isMultiTouchSupported() {
		return multiTouchSupported;
	}

	public void setMultiTouchSupported(boolean multiTouchSupported) {
		this.multiTouchSupported = multiTouchSupported;
	}

	public int getHolesPressed() {
		return holesPressed;
	}

	public void setHolesPressed(int holesPressed) {
		this.holesPressed = holesPressed;
	}	
	
	public void addHolePressed() {
		this.holesPressed++;
	}	
	
	public void removeHolePressed() {
		this.holesPressed--;
	}	
	
	public void update() {
		
		//End of game
//		if (this.holesPressed == this.numHoles )
//			Toast.makeText(, "MultiTouch detected --> Drag multiple Sprites with multiple fingers!", Toast.LENGTH_LONG).show();
//		
//		//Mostrar mensaje, presionar los puntos ya presionados
//		if (this.numLostClose >0){
//			Toast.makeText(, "", Toast.LENGTH_LONG).show();
//		}
//		
//		//Se acabo el tiempo para el jugador
//		if (this.timeByPlayer == MAX_TIME_BY_PLAYER){
//			Toast.makeText(, "", Toast.LENGTH_LONG).show();
//		}
//		
//		//Se acabo el tiempo para pulsar otra vez
//		if (this.timeForLostClosed == MAX_TIME_BY_LOST_CLOSE){
//			Toast.makeText(, "", Toast.LENGTH_LONG).show();
//		}
		
		
		

	}
	

}
